package com.party.web.biz.notify;

import com.google.common.base.Function;
import com.party.common.constant.Constant;
import com.party.common.paging.Page;
import com.party.common.utils.LangUtils;
import com.party.common.utils.StringUtils;
import com.party.core.model.activity.Activity;
import com.party.core.model.member.Member;
import com.party.core.model.notify.TargetTemplate;
import com.party.core.model.notify.TargetTemplateType;
import com.party.core.model.notify.TargetTemplateWithQuery;
import com.party.core.service.activity.IActivityService;
import com.party.core.service.crowdfund.IProjectService;
import com.party.core.service.crowdfund.ITargetTemplateService;
import com.party.core.service.member.IMemberService;
import com.party.core.service.notify.IInstanceService;
import com.party.web.web.dto.input.common.CommonQuery;
import com.party.web.web.dto.output.notify.TemplateOutput;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * 短信模板业务服务
 * Created by wei.li
 *
 * @date 2017/10/12 0012
 * @time 13:51
 */

@Service
public class MessageTemplateBizService {

    @Autowired
    private ITargetTemplateService targetTemplateService;

    @Autowired
    private IProjectService projectService;

    @Autowired
    private IActivityService activityService;

    @Autowired
    private IInstanceService instanceService;

    @Autowired
    private IMemberService memberService;

    /**
     * 初始化短信模板
     */
    public void init() {
        List<TargetTemplate> list = targetTemplateService.list(new TargetTemplate());
        for (TargetTemplate targetTemplate : list) {
            Activity activity = activityService.get(targetTemplate.getTargetId());
            targetTemplate.setInitiatorId(activity.getMember());
            targetTemplateService.update(targetTemplate);
        }
    }


    /**
     * 短信模板列表
     *
     * @param targetTemplateWithQuery 查询参数
     * @param page                    分页参数
     * @return 模板列表
     */
    public List<TemplateOutput> list(TargetTemplateWithQuery targetTemplateWithQuery, Page page) {

        if (null != targetTemplateWithQuery.getTimeType() && !new Integer(0).equals(targetTemplateWithQuery.getTimeType())) {
            CommonQuery commonQuery = new CommonQuery();
            commonQuery.setTimeType(targetTemplateWithQuery.getTimeType());
            targetTemplateWithQuery.setCreateStart(commonQuery.getCreateStart());
            targetTemplateWithQuery.setCreateEnd(commonQuery.getCreateEnd());
        }

        List<TargetTemplate> list = targetTemplateService.listWithQueryPage(targetTemplateWithQuery, page);
        List<TemplateOutput> outputList = LangUtils.transform(list, new Function<TargetTemplate, TemplateOutput>() {
            @Override
            public TemplateOutput apply(TargetTemplate targetTemplate) {
                TemplateOutput templateOutput = TemplateOutput.transform(targetTemplate);
                if (targetTemplate.getType() != null)
                {
                    TargetTemplateType targetTemplateType = TargetTemplateType.get(targetTemplate.getType());
                    if (targetTemplateType.getType().equals(0)) {
                        templateOutput.setModule("众筹");
                    } else {
                        templateOutput.setModule("活动");
                    }
                } else
                {
                    if (StringUtils.isNotEmpty(targetTemplate.getMemberIds()))
                    {
                        StringBuilder nicknames = new StringBuilder();
                        String memberIds = targetTemplate.getMemberIds();
                        String[] ids = memberIds.split(",");
                        for (String id : ids) {
                            Member member = memberService.get(id);
                            if (member != null)
                            {
                                nicknames.append(member.getRealname()).append(",");
                            }
                        }
                        String nicks = nicknames.toString();
                        if (StringUtils.isNotEmpty(nicks))
                        {
                            int i = nicks.lastIndexOf(",");
                            String nicksName = nicks.substring(0, i);
                            templateOutput.setMemberNickNames(nicksName);
                        }

                    }

                    templateOutput.setModule("行知公社");
                }

                Integer successNum = instanceService.countByResult(Constant.IS_SUCCESS, targetTemplate.getId());
                Integer failureNum = instanceService.countByResult(Constant.IS_FAIL, targetTemplate.getId());
                templateOutput.setSuccessNum(successNum);
                templateOutput.setFailureNum(failureNum);
                if (StringUtils.isNotEmpty(templateOutput.getMemberNickNames()))
                {
                    return templateOutput;
                } else
                {
                    return null;
                }

            }
        });
        return outputList;
    }
}
